<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .wrap{
            width: 400px;
            height: 300px;
            margin:  0 auto;
            border: 1px solid red;
            position: relative;
        }
        .wrap img{
            width: 400px;
            height: 300px;
        }
        span{
            display: inline-block;
            width: 20px;
            height: 20px;
            background-color: pink;
            border-radius: 50%;
        }
        p{
            width: 100%;
            position: absolute;
            bottom: 20px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="wrap">
        <img src="./img/1.jpg" alt="">
        <p>
            <span index="0"></span>
            <span index="1"></span>
            <span index="2"></span>
            <span index="3"></span>
        </p>
    </div>

    <script>
        // 1、获取元素
        var oImg = document.querySelector("img");
        var oSpan = document.querySelectorAll('span');

        // 案例： 图片路径通过数组进行存储

        // 2、加事件  小圆点加点击事件
        for(var i=0;i<oSpan.length;i++){
            // 自定义索引 ---保留for循环的下标
            oSpan[i].index = i;
            oSpan[i].onclick = function(){
                // 3、做什么事 -- 切换到对应的图片 
                console.log(this.index);//
                oImg.src = './img/'+ (this.index+1) +'.jpg';
            }
        }
    </script>
</body>
</html>